package newui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shwread.android.qysw10000241.R;

import java.util.ArrayList;
import java.util.List;

import logic.bean.RegionModuleBean;

/**
 * Created by lex on 2017/7/6.
 */

public class CategroyGridViewAdapter extends BaseAdapter{

    private Context context;
    private List<RegionModuleBean> list = new ArrayList<>();
    private int mIndex;
    private int mPageSize;

    public CategroyGridViewAdapter(Context context, List<RegionModuleBean> list, int index, int pageSize){
        this.context = context;
        this.list = list;
        this.mIndex = index;
        this.mPageSize = pageSize;
    }


    @Override
    public int getCount() {
        return list.size() > (mIndex + 1) * mPageSize ? mPageSize : (list.size() - mIndex * mPageSize);
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i + mIndex * mPageSize;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view==null){
            view = LayoutInflater.from(context).inflate(R.layout.item_enterprise_category,null);
            holder = new ViewHolder();
            holder.entrance_image = (ImageView) view.findViewById(R.id.entrance_image);
            holder.entrance_name = (TextView) view.findViewById(R.id.entrance_name);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }
        int pos = i + mIndex * mPageSize;
        Glide.with(context)
                .load(list.get(pos).getImgUrl())
                .error(R.drawable.atlas_default_img)
                .into(holder.entrance_image);
        holder.entrance_name.setText(list.get(pos).getModuleName());
        return view;
    }

    class ViewHolder{
        ImageView entrance_image;
        TextView entrance_name;
    }
}
